/*
Majority of code taken from AutoIT

Copyright 2006 Jonathon Bennett and the AutoIT Authors

http://www.autoitscript.com/autoit3/docs/credits.htm

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "stdafx.h"
#include "tokentypes.h"
#include "Interpreter.h"
#include "variabledomain.h"
#include "variant.h"
#include "Parser.h"
#include "errorcodes.h"
#include "Utility.h"



token Parser_ReturnMacro(char* name){
	token result;
	result.type = OPT_INT32_TOK;
	result.nValue = 0;

	if(strcmp(name, "CRLF") == 0){
		result.szValue = new char[3];
		strcpy(result.szValue, "\r\n");
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "LF") == 0){
		result.szValue = new char[2];
		strcpy(result.szValue, "\n");
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "CR") == 0){
		result.szValue = new char[2];
		strcpy(result.szValue, "\r");
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "TAB") == 0){
		result.szValue = new char[2];
		strcpy(result.szValue, "\t");
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "PARADIMEVERSION") == 0){
		result.szValue = new char[4];
		strcpy(result.szValue, "0.1");
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "PROGRAMFILESDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_RegReadString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion", "ProgramFilesDir", MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "COMMONFILESDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_RegReadString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion", "CommonFilesDir", MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "MYDOCUMENTSDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_RegReadString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Personal", MAX_PATH, result.szValue);
		Util_StripTrailingDir(result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "APPDATACOMMONDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_RegReadString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Common AppData", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "DESKTOPCOMMONDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_RegReadString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Common Desktop", _MAX_PATH, result.szValue);
		if (result.szValue[0] == '\0')
			Util_RegReadString(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Desktop", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "DOCUMENTSCOMMONDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_RegReadString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Common Documents", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "FAVORITESCOMMONDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
			Util_RegReadString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Common Favorites", _MAX_PATH, result.szValue);
			if (result.szValue[0] == '\0')
				Util_RegReadString(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Favorites", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "PROGRAMSCOMMONDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
			Util_RegReadString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Common Programs", _MAX_PATH, result.szValue);
			if (result.szValue[0] == '\0')
				Util_RegReadString(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Programs", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "STARTMENUCOMMONDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
			Util_RegReadString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Common Start Menu", _MAX_PATH, result.szValue);
			if (result.szValue[0] == '\0')
				Util_RegReadString(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Start Menu", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "STARTUPCOMMONDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
			Util_RegReadString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Common Startup", _MAX_PATH, result.szValue);
			if (result.szValue[0] == '\0')
				Util_RegReadString(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Startup", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "APPDATADIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_RegReadString(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "AppData", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "DESKTOPDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_RegReadString(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Desktop", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "FAVORITESDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_RegReadString(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Favorites", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "PROGRAMSDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_RegReadString(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Programs", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "STARTMENUDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_RegReadString(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Start Menu", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "STARTUPDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_RegReadString(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Startup", _MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}
	else if(strcmp(name, "COMPUTERNAME") == 0){
		result.szValue = new char[MAX_PATH+1];
		DWORD dwTemp = _MAX_PATH;
		GetComputerName(result.szValue, &dwTemp);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "WINDOWSDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		GetWindowsDirectory(result.szValue, _MAX_PATH);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "SYSTEMDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		GetSystemDirectory(result.szValue, _MAX_PATH);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "WORKINGDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
		GetCurrentDirectory(_MAX_PATH, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}
	else if(strcmp(name, "IPADDRESS1") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_GetIPAddress(1, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "IPADDRESS2") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_GetIPAddress(2, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "IPADDRESS3") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_GetIPAddress(3, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "IPADDRESS4") == 0){
		result.szValue = new char[MAX_PATH+1];
		Util_GetIPAddress(4, result.szValue);
		result.type = OPT_STRINGLITERAL_TOK;
	}
	else if(strcmp(name, "TEMPDIR") == 0){
		result.szValue = new char[MAX_PATH+1];
			GetTempPath(_MAX_PATH, result.szValue);
			Util_StripTrailingDir(result.szValue);		// Remove trailing backslash
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "USERNAME") == 0){
		result.szValue = new char[MAX_PATH+1];
			DWORD dwTemp = _MAX_PATH;
			GetUserName(result.szValue, &dwTemp);
		result.type = OPT_STRINGLITERAL_TOK;
	}
	else if(strcmp(name, "HOMEDRIVE") == 0){
		result.szValue = new char[MAX_PATH+1];strcpy(result.szValue, "");
		GetEnvironmentVariable("HOMEDRIVE", result.szValue, _MAX_PATH);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "HOMEPATH") == 0){
		result.szValue = new char[MAX_PATH+1];strcpy(result.szValue, "");
		GetEnvironmentVariable("HOMEPATH", result.szValue, _MAX_PATH);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "HOMESHARE") == 0){
		result.szValue = new char[MAX_PATH+1];strcpy(result.szValue, "");
		GetEnvironmentVariable("HOMESHARE", result.szValue, _MAX_PATH);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "LOGONSERVER") == 0){
		result.szValue = new char[MAX_PATH+1];strcpy(result.szValue, "");
		GetEnvironmentVariable("LOGONSERVER", result.szValue, _MAX_PATH);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "LOGONDOMAIN") == 0){
		result.szValue = new char[MAX_PATH+1];strcpy(result.szValue, "");
		GetEnvironmentVariable("USERDOMAIN", result.szValue, _MAX_PATH);
		result.type = OPT_STRINGLITERAL_TOK;
	}else if(strcmp(name, "LOGONDNSDOMAIN") == 0){
		result.szValue = new char[MAX_PATH+1];strcpy(result.szValue, "");
		GetEnvironmentVariable("USERDNSDOMAIN", result.szValue, _MAX_PATH);
		result.type = OPT_STRINGLITERAL_TOK;
	}




	return result;
}